package br.edu.ufcg.unknown.semantico;

import java.util.HashMap;
import java.util.Map;

import br.edu.ufcg.unknown.exception.AnalisadorSemanticoUnknownRuntimeException;
import br.edu.ufcg.unknown.lexico.Token;

public class TabelaDeSimbolos {

	Map<String, Atributo> simbolos;

	public TabelaDeSimbolos() {

		this.simbolos = new HashMap<String, Atributo>();

	}

	public void addSimbolo(Token tk, Tipo tipo) {
		Atributo at = new Atributo(tk.getLexema(), tipo);

		if (getAtributo(tk) != null) {
			// TODO lançar erro
			throw new AnalisadorSemanticoUnknownRuntimeException(tk, true);
		} else {
			this.simbolos.put(at.getNome(), at);
		}

	}

	public Atributo getAtributo(Token tk) {
		return tk != null ? this.simbolos.get(tk.getLexema()) : null;
	}

	public Tipo getTipo(Token tk) {

		Atributo at = getAtributo(tk);
		if (at != null)
			return at.getTipo();
		return null;
	}
}
